package com.sparkseries.service.impl;

import com.sparkseries.dao.SysPositionMapper;
import com.sparkseries.domain.SysPosition;
import com.sparkseries.domain.vo.SysPositionVO;
import com.sparkseries.dto.SysPositionDTO;
import com.sparkseries.service.SysPositionService;
import com.sparkseries.common.util.domain.Result;
import com.sparkseries.common.util.tool.ObjectConvertTool;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 岗位管理
 */
@Service
public class SysPositionServiceImpl implements SysPositionService {

    @Resource
    private SysPositionMapper dao;

    /**
     * 创建岗位
     *
     * @param
     * @return
     */
    @Override
    public Result<?> createPositions(List<SysPositionDTO> sysPositionsDTO) {

        ArrayList<SysPositionDTO> existedLists = new ArrayList<>();
        List<SysPositionDTO> lists = new ArrayList<>();

        if (sysPositionsDTO == null || sysPositionsDTO.isEmpty()) {
            return Result.error("你没有输入数据");
        } else {
            for (SysPositionDTO position : sysPositionsDTO) {
                if (dao.getPositionById(position.getPositionId()) == null) {
                    lists.add(position);
                } else {
                    existedLists.add(position);
                }
            }

            List<SysPosition> sysPositions = ObjectConvertTool.originsConvert(sysPositionsDTO, SysPosition.class);
            if (sysPositionsDTO.size() == lists.size()) {
                if (dao.insertPositions(sysPositions) != 0) {
                    return Result.ok("添加成功");
                } else {
                    return Result.error("添加失败");
                }
            } else if (sysPositions.size() == existedLists.size()) {
                return Result.error("你想要创建的部门全部已经存在了创建失败");
            } else {
                List<SysPosition> vos = ObjectConvertTool.originsConvert(lists, SysPosition.class);
                if (dao.insertPositions(vos) != 0) {
                    return Result.error("你创建的部门中有一部分存已存在无法再创建 已存在部门有:" + existedLists);
                } else {
                    return Result.error("添加失败");
                }
            }
        }
    }

    /**
     * 获取所有岗位
     *
     * @return
     */
    @Override
    public Result<List<SysPositionVO>> getAllPosition() {
        List<SysPosition> allPositions = dao.getAllPositions();
        if (allPositions != null) {
            return Result.ok(ObjectConvertTool.originsConvert(allPositions, SysPositionVO.class));
        } else {
            return Result.error("获取失败");
        }
    }

    /**
     * 删除岗位
     *
     * @param ids
     * @return
     */
    @Override
    public Result<?> deletePositions(List<Integer> ids) {
        if (dao.deletePositions(ids) != 0) {
            return Result.ok(ids);
        } else {
            return Result.error();
        }

    }

    /**
     * 修改岗位
     *
     * @param position
     * @return
     */
    @Override
    public Result<?> updatePosition(SysPositionDTO position) {
        SysPosition sysPosition = ObjectConvertTool.originConvert(position, SysPosition.class);
        if (dao.updatePosition(sysPosition) != 0) {
            return Result.ok("修改成功");
        } else {
            return Result.error("修改失败");
        }
    }

    /**
     * 通过id获取岗位
     *
     * @param id
     * @return
     */
    @Override
    public Result<SysPositionVO> getPositionById(Integer id) {
        if (id == null) {
            return Result.error("你没有输入id");
        } else {
            SysPosition position = dao.getPositionById(id);
            if (position != null) {
                return Result.ok(ObjectConvertTool.originConvert(position, SysPositionVO.class));
            } else {
                return Result.error("你输入的id无效");
            }
        }

    }

    @Override
    public Result<List<SysPositionVO>> findPositionByCondition(SysPositionDTO position) {
        List<SysPosition> positions = dao.findPositionByCondition(ObjectConvertTool.originConvert(position, SysPosition.class));
        if (positions.isEmpty()) {
            return Result.error("你输入的条件没有匹配到任何岗位");
        } else {
            return Result.ok(ObjectConvertTool.originsConvert(positions, SysPositionVO.class));
        }
    }
}
